<?php

declare(strict_types=1);

namespace App\Service\Logic;

use App\Service\Business\RoleBusiness;
use App\Validator\CommonValidator;
use App\Validator\RoleValidator;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 角色-逻辑类
 */
class RoleLogic extends BaseLogic
{
        #[Inject]
        protected RoleBusiness $RoleBusiness;

        #[Inject]
        protected RoleValidator $RoleValidator;

        #[Inject]
        protected CommonValidator $CommonValidator;

        /**
         * 分页
         * @return array
         * @throws Exception
         */
        public function paginate(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['page', 'limit']);
                return $this->RoleBusiness->paginate();
        }

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function create(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['sort']);
                $this->RoleValidator->validate($this->request->all(), ['name', 'status']);
                $this->RoleValidator->uniqueFieldName($this->request->all(), 0);
                return $this->RoleBusiness->create();
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function update(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['sort']);
                $this->RoleValidator->validate($this->request->all(), ['roleId', 'name', 'status']);
                $this->RoleValidator->uniqueFieldName($this->request->all(), $this->request->input('roleId'));
                return $this->RoleBusiness->update();
        }
}